<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-08
 * Time: 22:42
 */

namespace app\shell\controller;


use app\tools\MIniprogram;
use think\Db;

class Msg {

    function __construct()
    {
        error_reporting(0);
    }

    function companyResult(){

        $list = db('company')->where(['is_send'=> 0])->field('id, is_send, status, reason')->select();
        $arr = [];

        foreach ($list as $v){

            $status = $v['status'];

            if($status == 3 || $status == 4){

                $content = "企业认证";
                $result = $status==3?"审核通过":"审核不通过";
                $time = date('Y-m-d H:i:s');

                $user = db('form_data')->where(['status'=> 1, 'create_time'=>['>', time() - 604800], 'company_id'=> $v['id']])->field("openid, form_id, id")->order('create_time')->select();

                foreach ($user as $vv){

                    if(in_array($vv['openid'], $arr)) continue;
                    else $arr[] = $vv['openid'];

                    MIniprogram::sendTemplate([
                        'openid'=> $vv['openid'],
                        'template_id'=> 'MFUizOdcN95F5qa9N7hkWVY3q6RKtK_qxs8JkFqbPHs',
                        'form_id'=> $vv['form_id'],
                        'data'=> [
                            'keyword1'=> ['value'=> $content],
                            'keyword2'=> ['value'=> $result],
                            'keyword3'=> ['value'=> $status==3?'':$v['reason']],
                            'keyword4'=> ['value'=> $time],
                        ]
                    ]);

                    db('form_data')->where(['id'=> $vv['id']])->delete();
                }

                db('company')->where(['id'=> $v['id']])->update(['is_send'=> 1]);

            }
        }

    }

    function jobResult(){

        $list = db('job')->where(['is_send'=> 0])->field('id, is_send, status, reason, company_id')->select();
        $arr = [];

        foreach ($list as $v){

            $status = $v['status'];

            if($status == 2 || $status == 4){

                $content = "招工审核";
                $result = $status==2?"审核通过":"审核不通过";
                $time = date('Y-m-d H:i:s');

                $user = db('form_data')->where(['status'=> 1, 'create_time'=>['>', time() - 604800], 'company_id'=> $v['company_id']])->field("openid, form_id, id")->order('create_time')->select();

                foreach ($user as $vv){

                    if(in_array($vv['openid'], $arr)) continue;
                    else $arr[] = $vv['openid'];

                    MIniprogram::sendTemplate([
                        'openid'=> $vv['openid'],
                        'template_id'=> 'MFUizOdcN95F5qa9N7hkWVY3q6RKtK_qxs8JkFqbPHs',
                        'form_id'=> $vv['form_id'],
                        'data'=> [
                            'keyword1'=> ['value'=> $content],
                            'keyword2'=> ['value'=> $result],
                            'keyword3'=> ['value'=> $status==2?'':$v['reason']],
                            'keyword4'=> ['value'=> $time],
                        ]
                    ]);

                    db('form_data')->where(['id'=> $vv['id']])->delete();
                }

                db('job')->where(['id'=> $v['id']])->update(['is_send'=> 1]);

            }
        }

    }

    // 工作超7天下架

    function jobDown(){

//        $list = db('job')->where(['is_send'=> 1, 'publish_time'=>['<', date('Y-m-d H:i:s', time() - 604400)], 'status'=> 2])->field('id, name, status, reason, company_id')->select();
//        echo db('job')->getLastSql();
        $sql = "select id, name, status, reason, company_id from c_job where is_send = 1 and status = 2 and UNIX_TIMESTAMP(publish_time) + valid_time - 400 < ".time();

        $list = Db::query($sql);

        $arr = [];

//        dump($list);
        foreach ($list as $v){

            $time = date('Y-m-d H:i:s');
            $name = $v['name'];

            $user = db('form_data')->where(['status'=> 1, 'create_time'=>['>', time() - 604800], 'company_id'=> $v['company_id']])->field("openid, form_id, id")->order('create_time')->select();

            foreach ($user as $vv){

                if(in_array($vv['openid'], $arr)) continue;
                else $arr[] = $vv['openid'];

                MIniprogram::sendTemplate([
                    'openid'=> $vv['openid'],
                    'template_id'=> 'zdLOMfG1zB3ST-CHE1PdjVGOmTYYQsYHpmwmf4rekCQ',
                    'form_id'=> $vv['form_id'],
                    'data'=> [
                        'keyword1'=> ['value'=> $name],
                        'keyword2'=> ['value'=> $time],
                    ]
                ]);

                db('form_data')->where(['id'=> $vv['id']])->delete();
            }

            db('job')->where(['id'=> $v['id']])->update(['is_send'=> 2, "status"=> 3]);

        }

    }

}